package Client;

import Admin.AdminWindow;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

@SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener {
	private ArrayList<Reciever> groupChats = new ArrayList<Reciever>();
	private ArrayList<Reciever> peopleChats = new ArrayList<Reciever>();
	List projectGroups, projectUsers;
	JTextField chatListener;
	Reciever secetedReciever = null;
	JCheckBox chatMessageImportance, showOffline;
	JMenuBar chatMenu;
	JScrollPane openWindow;

	boolean isProjectSelected;

	ClientSocket clientSocket = null;
	Client client = null;

	public GUI(Client client, ClientSocket clientSocket) {
		this.client = client;
		this.clientSocket = clientSocket;
		this.initInterface();
                
                
	}

	public void close() {

		System.out.println("DISPOSE");
		this.dispose();

	}

	private void initInterface() {

		setTitle("PSVA");
		setSize(600, 400);
		Container chatWindow = this.getContentPane();
		chatWindow.setLayout(new BorderLayout());
		chatWindow.setFont(new Font("Verdana", Font.PLAIN, 14));

		chatMenu = new JMenuBar();
		JMenu meniu = new JMenu("Meniu");
		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('x');
		meniu.add(exitItem);

		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				client.close();
			}
		});
		this.setJMenuBar(chatMenu);
		chatMenu.add(meniu);

		JPanel listsPanel = new JPanel();
		listsPanel.setLayout(new BorderLayout());

                projectGroups = new List(10, false);
		projectGroups.addActionListener(this);
                listsPanel.add("East", projectGroups);
		JPanel userPanel = new JPanel();
		userPanel.setLayout(new BorderLayout());
		projectUsers = new List(10, false);
		projectUsers.addActionListener(this);
		showOffline = new JCheckBox("Tik prisijungusius");
		showOffline.doClick();
		showOffline.addActionListener(this);
		userPanel.add("South", showOffline);
		userPanel.add("Center", projectUsers);

		listsPanel.add("West", userPanel);

		JPanel userBottom = new JPanel();
		userBottom.setLayout(new BorderLayout());
		chatListener = new JTextField();
		chatListener.addActionListener(this);
		chatMessageImportance = new JCheckBox("Svarbi žinutė");
		userBottom.add("Center", chatListener);
		userBottom.add("East", chatMessageImportance);

		chatWindow.add("East", listsPanel);
		chatWindow.add("South", userBottom);
		chatWindow.setMinimumSize(new Dimension(600, 400));
                if (client.admin) {
                    this.addFunctions();
                }
		ArrayList<String> test = new ArrayList<String>();
		this.setGroupChats(test);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
                

	}

	private void addFunctions() {
		JButton admButton = new JButton("Administravimas");
                admButton.addActionListener(new ActionListener() {
                        @Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("admin");
                                AdminWindow adm = new AdminWindow();
                                adm.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                                adm.addWindowListener( new WindowAdapter(){
                                    @Override
                                    public void windowClosing(WindowEvent e){
                                        clientSocket.requestAllProjects();
                                        clientSocket.requestAllUsers();
                                        //initInterface();
                                    }
                                });
                            // Pridejimo kodas
			}
		});
		

		chatMenu.add(admButton);
	}

	public boolean isImportatMesage() {
		return (chatMessageImportance.isSelected());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == chatListener) {
			if (secetedReciever != null) {
				String text = chatListener.getText();
				String where = secetedReciever.name;
				if (isProjectSelected == true) {
					clientSocket.sendMessageToProject(where, text, isImportatMesage());
				} else {
					clientSocket.sendMessageToClient(where, text, isImportatMesage());
                                }
				// texta siust nu dar cia galima naudot ta isImporrtantpries
				// siunciant
			}
			chatListener.setText("");
		}
		if (e.getSource() == projectGroups) {
			this.setSecetedReciever(projectGroups.getSelectedItem());
			try {
				clientSocket.requestProjectUsers(projectGroups
						.getSelectedItem());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			// pist prota kad atsiustu to projekto darbuotojus ir naudot
			// showPeopleChats(ArrayList<String> peopleNames)
		}
		if (e.getSource() == projectUsers) {
			this.setSecetedReciever(projectUsers.getSelectedItem());
		}
	}

	public void setGroupChats(ArrayList<String> groupNames) {
                groupChats.clear();
		for (String name : groupNames) {
			groupChats.add(new Reciever(name));
		}
		this.showGroupChats();
	}

	public void setPeopleChats(ArrayList<String> peopleNames) {
                peopleChats.clear();
		for (String name : peopleNames)
			peopleChats.add(new Reciever(name));
	}

	@SuppressWarnings("deprecation")
	public void showGroupChats() {
		projectGroups.clear();
		for (Reciever chatsG : groupChats) {
			projectGroups.addItem(chatsG.name);
		}
	}

	@SuppressWarnings("deprecation")
	public void showPeopleChats(ArrayList<String> peopleNames) {
		projectUsers.clear();
		for (Reciever chatP : peopleChats) {
			for (String name : peopleNames) {
				if (name.equals(chatP.name)) {
					if (!(showOffline.isSelected()) || (chatP.isOnline))
						projectUsers.addItem(chatP.name);

				}
			}
		}
	}

	public void setSecetedReciever(String recieverNames) {
		if (openWindow != null)
			this.remove(openWindow);
		JTextArea temp = null;
		for (Reciever chatsG : groupChats)
			if (chatsG.name.equals(recieverNames)) {
				temp = chatsG.getArea();
				secetedReciever = chatsG;
				isProjectSelected = true;
			}
		for (Reciever chatsP : peopleChats)
			if (chatsP.name.equals(recieverNames)) {
				temp = chatsP.getArea();
				secetedReciever = chatsP;
				isProjectSelected = false;
			}
		openWindow = new JScrollPane(temp);
		openWindow
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		openWindow
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		openWindow.setBounds(0, 0, 345, 325);
		this.add("Center", openWindow);
		this.setVisible(true);

	}

	public void write(String forWhom, String what) {
		for (Reciever chatsG : groupChats)
			if (chatsG.name.equals(forWhom))
				chatsG.write(what);
		for (Reciever chatsP : peopleChats)
			if (chatsP.name.equals(forWhom))
				chatsP.write(what);

	}

	public void updateOfflinePeople(ArrayList<String> peopleOnlineNames) {
		try {
			clientSocket.requestProjectUsers(projectGroups.getSelectedItem());
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		for (Reciever people : peopleChats) {
			people.isOnline = true;
			if (peopleOnlineNames.contains(people.name))
				people.isOnline = false;
		}
	}
}